package vn.orderonline.web.admin.controller;

import java.security.Principal;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import vn.orderonline.com.core.business.acountadmin.IAccountAdminManager;

@Controller
@RequestMapping(value = "/adminaccount")
public class AccountAdminController {

	private static final Logger LOGGER = Logger
			.getLogger(AccountAdminController.class);

	@Autowired
	private IAccountAdminManager iAccountAdminManager;

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login(ModelMap model, Principal principal,
			HttpServletRequest request) {
//		if (request.getCookies() != null) {
//			Cookie[] cookies = request.getCookies();
//			for (Cookie cookie : cookies) {
//				if ("JSESSIONID".equals(cookie.getName())) {
//					
//				} else if ("SPRING_SECURITY_REMEMBER_ME_COOKIE".equals(cookie
//						.getName())) {
//					return "redirect:/adminaccount/home";
//				}
//			}
//		}
		if (request.getUserPrincipal() != null) {
			return "redirect:/adminaccount/home";
		}

		LOGGER.info("START CREATE FORM LOGIN!");
		try {

		} catch (Exception e) {
			LOGGER.error("ERROR WHEN CREATE FORM LOGIN!", e);
		}
		LOGGER.info("END CREATE FORM LOGIN!");

		return "/home/login";
		//return "/layouts/default";
	}

	@RequestMapping(value = "/home", method = RequestMethod.GET)
	public String loginSuccess(Principal principal, HttpServletRequest request) {
		LOGGER.info("START GO TO HOME PAGE AFTER LOGIN SUCCESSFULL!");
		try {
		} catch (Exception e) {
			LOGGER.error(
					"ERROR WHEN GO TO HOME PAGE WHEN LOGGIN SUCCESSFULLY!", e);
		}
		LOGGER.info("END GO TO HOME PAGE AFTER LOGIN PAGE!");
		return "/home/home";
	}

	@RequestMapping(value = "/fail2login", method = RequestMethod.GET)
	public String loginError() {
		LOGGER.info("YOU HAVE LOGIN FAIL!");
		try {

		} catch (Exception e) {

		}
		return "/home/login";
	}

	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String logout(Principal principal) {
		LOGGER.info("YOU HAVE JUST LOGOUT!");
		if (principal != null) {
			System.out.println("username in logout : " + principal.getName());
		}
		try {
		} catch (Exception e) {

		}
		return "/home/login";
	}

	@RequestMapping(value = "/checkexistedaccountadmin", method = RequestMethod.GET)
	@ResponseBody
	public String checkExistedAccountAdmin(String username, String password) {
		LOGGER.info("START CHECK EXISTED ACCOUNT!");
		Boolean flag = false;
		try {
			flag = iAccountAdminManager.checkExistedAccountAdmin(username,
					password);
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN CHECK EXISTED ACCOUNT!", e);
		}
		LOGGER.info("END CHECK EXISTED ACCOUNT!" + flag.toString());
		return flag.toString();
	}
}
